﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Classes;

namespace OfficeManager_v0._1
{
    public partial class ReassignWorker : Form
    {
        Organization mOrganization;
        Worker oldWorker;

        public ReassignWorker(Organization aOrganization, Worker aWorker)
        {
            mOrganization = aOrganization;
            oldWorker = aWorker;

            InitializeComponent();

            comboBoxDepartment.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBoxOffice.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBoxProjects.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBoxTelephone.DropDownStyle = ComboBoxStyle.DropDownList;

            PopulateComboBoxes();
            textBoxPosition.Text = oldWorker.CurrentJob.Position;
            textBoxPayment.Text = oldWorker.CurrentJob.Payment.ToString();
            textBoxHireDate.Text = oldWorker.CurrentJob.HireDate;

            if (oldWorker.Department != null)
                comboBoxDepartment.SelectedItem = oldWorker.Department.Name + " ID:" + oldWorker.Department.ID;
            if (oldWorker.CurrentProject != null)
                comboBoxProjects.SelectedItem = oldWorker.CurrentProject;
            if (oldWorker.Office != null)
                comboBoxOffice.SelectedItem = oldWorker.Office.Name + " ID: " + oldWorker.Office.ID;
            if (oldWorker.TelephonePost != null)
                comboBoxTelephone.SelectedItem = oldWorker.TelephonePost.TelephoneNumber.Number;
            comboBoxDepartment_SelectedIndexChanged(null, new EventArgs());
        }

        private void PopulateComboBoxes ()
        {
            comboBoxDepartment.Enabled = (mOrganization.Departments != null);
            foreach (Department dep in mOrganization.Departments)
            {
                comboBoxDepartment.Items.Add(dep.Name + " ID:" + dep.ID);
            }
            comboBoxOffice.Enabled = false;
            comboBoxProjects.Enabled = false;
            comboBoxTelephone.Enabled = false;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            try
            { 
                Department target = null;
                if (comboBoxDepartment.GetItemText(comboBoxDepartment.SelectedItem) != "")
                    target = mOrganization.Departments.Find(
                        (dep) =>
                        {
                            return (dep.Name + " ID:" + dep.ID == comboBoxDepartment.GetItemText(comboBoxDepartment.SelectedItem));
                        });

                Office office = null;
                if (comboBoxOffice.GetItemText(comboBoxOffice.SelectedItem) != "")
                    office= mOrganization.Offices.Find(
                        (off) =>
                        {
                            return off.Name + " ID:" + off.ID == comboBoxOffice.GetItemText(comboBoxOffice.SelectedItem);
                        });

                Project project = null;
                if (comboBoxProjects.GetItemText(comboBoxProjects.SelectedItem) != "")
                    project = mOrganization.Projects.Find(
                        (proj) =>
                        {
                            return string.Compare(proj.Subject, comboBoxProjects.GetItemText(comboBoxProjects.SelectedItem)) == 0;
                        });

                TelephonePost post = null;
                if (comboBoxTelephone.GetItemText(comboBoxTelephone.SelectedItem) != "")
                    post = office.TelephonePost.Find(
                        (p) =>
                        {
                            return string.Compare(p.TelephoneNumber.Number,
                                comboBoxTelephone.GetItemText(comboBoxTelephone.SelectedItem)) == 0;
                        });

                oldWorker.UnassignWorker();
                
                if (oldWorker.Department != null)
                    oldWorker.Department.Workers.Remove(oldWorker);
                if (oldWorker.TelephonePost != null)
                    oldWorker.TelephonePost = null;

                oldWorker.CurrentProject = project;
                oldWorker.Office = office;
                oldWorker.TelephonePost = post;
                oldWorker.Department = target;

                oldWorker.CurrentJob = new Worker.JobEntry(textBoxPosition.Text,
                    float.Parse(textBoxPayment.Text), textBoxHireDate.Text);

                if (target != null) target.Workers.Add(oldWorker);
            }
            catch (Exception exc)
            {
                //Console.WriteLine(exc.Message);
                MessageBox.Show("Въведените данни са некоректни!");
            }

            this.Close();
            this.Dispose();
        }

        private void comboBoxDepartment_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Department target = mOrganization.Departments.Find(
                    (dep) =>
                    {
                        return (dep.Name + " ID:" + dep.ID == comboBoxDepartment.GetItemText(comboBoxDepartment.SelectedItem));
                    });

                if (target == null && comboBoxDepartment.GetItemText(comboBoxOffice.SelectedItem) != "")
                    throw new Exception();

                comboBoxOffice.Enabled = (target != null && target.Offices.Count != 0);
                comboBoxOffice.SelectedText = "";
                comboBoxOffice.Items.Clear();
                foreach (Office office in target.Offices)
                {
                    comboBoxOffice.Items.Add(office.Name + " ID:" + office.ID);
                }

                comboBoxProjects.Enabled = (target != null && target.Projects.Count != 0);
                comboBoxProjects.Items.Clear();
                comboBoxProjects.SelectedText = "";
                foreach (Project project in target.Projects)
                {
                    comboBoxProjects.Items.Add(project.Subject);
                }
            }
            catch (Exception exc)
            {
                //DO NOT ADD ANYTHING HERE!!!
            }
        }

        private void comboBoxOffice_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Office target = mOrganization.Offices.Find(
                    (off) =>
                    {
                        return off.Name + " ID:" + off.ID == comboBoxOffice.GetItemText(comboBoxOffice.SelectedItem);
                    });

                if (target == null && comboBoxOffice.GetItemText(comboBoxOffice.SelectedItem) != "")
                    throw new Exception();

                comboBoxTelephone.Enabled = false;//(target != null && target.TelephonePost.Count != 0);
                comboBoxTelephone.Items.Clear();
                comboBoxTelephone.SelectedText = "";
                foreach (TelephonePost post in target.TelephonePost)
                {
                    comboBoxTelephone.Enabled = true;
                    comboBoxTelephone.Items.Add(post.TelephoneNumber.Number);
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show("Въведените данни са некоректни!");
            }
        }
    }
}
